const validator = require('validator')

// 创建用户校验
const validteCreateUser = (email, username, password) => {
    try {
        console.log("validteCreateUser",email, username, password);

        let error = {}//数据校验：错误对象结果

        if (!email) {//判断underfin   null
            error.email = "邮箱不能为空"
        }

        if (validator.isEmpty(username)) {
            error.username = "用户名不能为空"
        }

        if (validator.isEmpty(password)) {
            error.password = "密码不能为空"
        }

        if (email && !validator.isEmail(email)) {
            error.email = "邮箱格式不正确123"
        }
      
        let validate = Object.keys(error).length < 1  //校验结果标记：true-通过  false-失败
        // console.log(validate);
        return { error, validate }
        
    } catch (error) {
        console.log(error);
    }
}
//用户登录校验
const validateUserLogin = (email, password) => {
    try {

        let error = {}//数据校验：错误对象结果

        if (!email) {//判断underfin   null
            error.email = "邮箱不能为空"
        }

        if (validator.isEmpty(password)) {
            error.password = "密码不能为空"
        }

        if (email && !validator.isEmail(email)) {
            error.email = "邮箱格式不正确123"
        }
      
        let validate = Object.keys(error).length < 1  //校验结果标记：true-通过  false-失败
        // console.log(validate);
        return { error, validate }
        
    } catch (error) {
        console.log(error);
    }
}
module.exports = {
    validteCreateUser,
    validateUserLogin
}